<?php

class ProprieteValueDAO extends DAO{


	function getById($id){
		try
		{
			$id =Validator::escapeSql($id);
			$query="select * from PROPRIETE_VALUES where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$langueDAO = $this->factory->getLangueDAO();
			$entiteDAO = $this->factory->getEntiteDAO();
			$ListValueDAO = $this->factory->getListeValueDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new ProprieteValue((int)$Bean->ID);
				$thebean->setProprieteId((int)$Bean->PROP_ID);
				$thebean->setEntiteId((int)$Bean->ENTITE_ID);
				$thebean->setValueShort($Bean->VALUE_SHORT);
				$thebean->setValueText($Bean->VALUE_TEXT);
				if(!is_null($Bean->VALUE_DATE_FROM)){
					list($year,$month,$day)=split('[/,-]',$Bean->VALUE_DATE_FROM);
					$thebean->setDateFrom($day, $month, $year);
				}
				if(!is_null($Bean->VALUE_DATE_TO)){
					list($year,$month,$day)=split('[/,-]',$Bean->VALUE_DATE_TO);
					$thebean->setDateTo($day, $month, $year);
				}
				 
				$thebean->setDuration($Bean->DURATION);
				$thebean->setChecked($Bean->CHECKED);
				$thebean->setLangue($langueDAO->getById((int)$Bean->LNG_ID));
				if(!is_null($Bean->LIST_VALUE_ID)){
					$thebean->setListeValue($ListValueDAO->getById((int)$Bean->LIST_VALUE_ID));
				}
				 
				$thebean->setReferencedEntite($entiteDAO->getById((int)$Bean->REFERENCED_ENTITE_ID));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function remove($Bean){
		try{
			$query="delete from PROPRIETE_VALUES where id='".Validator::escapeSql($Bean->getId())."';";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			$query = "update PROPRIETE_VALUES set prop_id=".Validator::escapeSql($Bean->getProprieteId()).", lng_id=".Validator::escapeSql($Bean->getLangue()->getId()).",value_short='".Validator::escapeSql($Bean->getValueShort())."',value_text='".Validator::escapeSql($Bean->getValueText())."',";
			$query.="entite_id=".Validator::escapeSql($Bean->getEntiteId()).", referenced_entite_id=".Validator::escapeSql($Bean->getReferencedEntite()->getId()).",list_value_id=".Validator::escapeSql($Bean->getListeValue()->getId()).",value_date_from=DATE(".Validator::escapeSql($Bean->getDateFrom())."),value_date_to=DATE('".Validator::escapeSql($Bean->getDateTo())."'),duration=".Validator::escapeSql($Bean->getDuration()).",checked=".Validator::escapeSql($Bean->getChecked());
			$query.=" where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;
			 
		}
		return $bool;
	}
	function add($Bean){
		 
		try{
			 
			if (is_null($Bean->getId())){
				$insertQuery = "insert into PROPRIETE_VALUES (prop_id,lng_id,value_short,value_text,entite_id,referenced_entite_id,list_value_id,value_date_from,value_date_to,duration,checked) values";
				$insertQuery .=" (".Validator::escapeSql($Bean->getProprieteId()).",".Validator::escapeSql($Bean->getLangue()->getId()).",'".Validator::escapeSql($Bean->getValueShort())."','".Validator::escapeSql($Bean->getValueText())."',".Validator::escapeSql($Bean->getEntiteId()).",".Validator::escapeSql($Bean->getReferencedEntite()->getId()).",".Validator::escapeSql($Bean->getListeValue()->getId()).",DATE(".Validator::escapeSql($Bean->getDateFrom())."),DATE('".Validator::escapeSql($Bean->getDateTo())."'),".Validator::escapeSql($Bean->getDuration()).",".Validator::escapeSql($Bean->getChecked()).");";
				$this->pdo->exec($insertQuery);
				$thebean = null;
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
			}
		}catch(Exception $e)    {$this->pdo->message;}
		return $thebean;
	}

	function All(){
		try
		{

			$BeanArray = array();
			$query="select * from PROPRIETE_VALUES;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				array_push($BeanArray,$thebean);
			}
				
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
	function getAllByProprieteByEntite($Entite,$Propriete){
		try
		{

			$BeanArray = array();
			$query="select * from PROPRIETE_VALUES where prop_id=".Validator::escapeSql($Propriete->getId())." and entite_id=".Validator::escapeSql($Entite->getId()).";";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				array_push($BeanArray,$thebean);
			}
				
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
}

?>